﻿package com.wsj.common.paymoney.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class Payment {

	public static String CreateUrl(String paygateway, String service,
			String sign_type, String out_trade_no, String input_charset,
			String partner, String agent, String key, String seller_email,
			String body, String subject, String price, String quantity,
			String show_url, String payment_type, String discount,
			String notify_url,String return_url,String logistics_type,String logistics_fee,String logistics_payment) {
		// String notify_url,��Ҫ����Ѳ���������ϵ�createurl
		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("out_trade_no", out_trade_no);
		params.put("show_url", show_url);
		params.put("quantity", quantity);
		params.put("partner", partner);
		params.put("agent", agent);
		params.put("payment_type", payment_type);
		params.put("discount", discount);
		params.put("body", body);
		params.put("price", price);
		params.put("notify_url", notify_url);
		params.put("return_url", return_url);
		params.put("seller_email", seller_email);
		params.put("logistics_type", logistics_type);
		params.put("logistics_fee", logistics_fee);
		params.put("logistics_payment", logistics_payment);
		params.put("subject", subject);
		params.put("_input_charset", input_charset);
		
		StringBuffer stringBuffer=new StringBuffer("");
//		String prestr = "";
		
		stringBuffer.append(key);
//		prestr = prestr + key;
		// System.out.println("prestr=" + prestr);

		String sign = Md5Encrypt.md5(getContent(params, key));
		
		StringBuffer stringBufferparameter=new StringBuffer("");

//		String parameter = "";
		stringBufferparameter.append(paygateway);
//		parameter = parameter + paygateway;

		List keys = new ArrayList<String>(params.keySet());
		for (int i = 0; i < keys.size(); i++) {
			try {
				stringBufferparameter.append(keys.get(i)
						+ "="
						+ URLEncoder.encode((String) params.get(keys.get(i)),
								input_charset) + "&");
//				parameter = parameter
//						+ keys.get(i)
//						+ "="
//						+ URLEncoder.encode((String) params.get(keys.get(i)),
//								input_charset) + "&";
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		stringBufferparameter.append("sign=" + sign + "&sign_type=" + sign_type);

//		parameter = parameter + "sign=" + sign + "&sign_type=" + sign_type;

		return stringBufferparameter.toString();

	}

	private static String getContent(Map<String,String> params, String privateKey) {
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		
		StringBuffer buffprestr=new StringBuffer("");

//		String prestr = "";

		for (int i = 0; i < keys.size(); i++) {
			String key = (String) keys.get(i);
			String value = (String) params.get(key);

			if (i == keys.size() - 1) {
				buffprestr.append(key + "=" + value);
//				prestr = prestr + key + "=" + value;
			} else {
				buffprestr.append(key + "=" + value + "&");
//				prestr = prestr + key + "=" + value + "&";
			}
		}

		return buffprestr.append(privateKey).toString();
//		return prestr + privateKey;
	}
}









